﻿Imports Framework.Data

Public Class DownloadsItems
    Private Shared mCache As New Generic.List(Of Entities.Downloads.Item)

    Shared Sub New()
        Try
            SyncLock mCache
                For Each clsItem As Entities.Downloads.Item In DataStore.DownloadsItemsEx.GetAll
                    mCache.Add(clsItem)
                Next
            End SyncLock
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub

    Public Shared Sub Save(ByVal item As Entities.Downloads.Item)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Save(item, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Save(ByVal item As Entities.Downloads.Item, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            Dim blnIsNew As Boolean = item.Id = 0
            DataStore.DownloadsItemsEx.Save(item, transactionScope)
            If blnIsNew Then mCache.Add(item)
        End SyncLock
    End Sub
    Public Shared Sub Delete(ByVal id As Integer)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Delete(id, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            DataStore.DownloadsItemsEx.Delete(id, transactionScope)
            mCache.Remove(GetById(id))
        End SyncLock
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Downloads.Item)
        SyncLock mCache
            Return mCache.ToList
        End SyncLock
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Downloads.Item
        SyncLock mCache
            Return (From f In mCache Where (f.Id = id) Select f).FirstOrDefault
        End SyncLock
    End Function
    Public Shared Function GetByUniqueId(ByVal uniqueId As String) As Entities.Downloads.Item
        SyncLock mCache
            Return (From f In mCache Where (f.UniqueId = uniqueId) Select f).FirstOrDefault
        End SyncLock
    End Function
End Class
